﻿using EasyFlow.Motion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kimd.DMC3800Card
{
    public class DMC3800CardCoordinateOperator : ICoordinate
    {
        public bool IsError { get; set; }

        public string LastError { get; set; }

        public bool CheckApiResult(short rtn, string errMessage)
        {
            if (rtn != 0)
            {
                //_isApiErr = true;
                //string errorStr = ErrorCode2ErrorStr(rtn);
                //MachineMessage.CallException(ExceptionType.FatalErr, "轴卡", $"[{rtn}{errorStr}]:{err}", MachineErrCode.CardErr);
                return false;
            }
            return true;
        }

        public bool IsBufferDone()
        {
            throw new NotImplementedException();
        }

        public bool IsCoordinateDone(short cardNo)
        {
            short rtn = 0;
            rtn += csLTDMC.LTDMC.dmc_check_done_multicoor((ushort)cardNo, 0);
            return CheckApiResult(rtn, "查询坐标系运动状态失败!");
        }

        public bool MoveArc(short cardNo, short axisNo1, short axisNo2, int[] distancesInPulse, int radiusInpulse, bool isCCW, AxisSpeed crdSpeed)
        {
            throw new NotImplementedException();
        }

        public bool MoveBufferLine(short cardNo, short axisNo1, short axisNo2, List<OneMoveSegment> points)
        {
            throw new NotImplementedException();
        }

        public bool MoveLine2(short cardNo, short axisNo1, short axisNo2, int[] distancesInPulse, AxisSpeed crdSpeed, MoveType moveType = MoveType.Absolute)
        {
            short rtn = 0;
            rtn += csLTDMC.LTDMC.dmc_set_vector_profile_multicoor((ushort)cardNo, 0, crdSpeed.VelStart, crdSpeed.Vel, crdSpeed.Acc, crdSpeed.Dec, crdSpeed.VelEnd);
            ushort[] axisList = new ushort[2] { (ushort)(axisNo1 - 1), (ushort)(axisNo2 - 1) };
            if (moveType == MoveType.Absolute)
                csLTDMC.LTDMC.dmc_line_multicoor((ushort)cardNo, 0, 2, axisList, distancesInPulse, 1);
            else
                csLTDMC.LTDMC.dmc_line_multicoor((ushort)cardNo, 0, 2, axisList, distancesInPulse, 0);//运动模式，0：相对坐标模式，1：绝对坐标模式
            //return CheckApiResult(rtn, "设置两轴直线插补失败!");
            return true;
        }

        public bool MoveLine2(short cardNo, short crdNo, short axisNo1, short axisNo2, int[] distancesInPulse, AxisSpeed crdSpeed, MoveType moveType = MoveType.Absolute)
        {
            throw new NotImplementedException();
        }

        public bool MoveLine3(short cardNo, short axisNo1, short axisNo2, short axisNo3, int[] distancesInPulse, AxisSpeed crdSpeed, MoveType moveType = MoveType.Absolute)
        {
            short rtn = 0;
            rtn += csLTDMC.LTDMC.dmc_set_vector_profile_multicoor((ushort)cardNo, 0, crdSpeed.VelStart, crdSpeed.Vel, crdSpeed.Acc, crdSpeed.Dec, crdSpeed.VelEnd);
            ushort[] axisList = new ushort[3] { (ushort)(axisNo1 - 1), (ushort)(axisNo2 - 1), (ushort)(axisNo3 - 1) };
            //运动模式，0：相对坐标模式，1：绝对坐标模式
            if (moveType == MoveType.Absolute)
                rtn += csLTDMC.LTDMC.dmc_line_multicoor((ushort)cardNo, 0, 3, axisList, distancesInPulse, 1);
            else
                rtn += csLTDMC.LTDMC.dmc_line_multicoor((ushort)cardNo, 0, 3, axisList, distancesInPulse, 0);
            return CheckApiResult(rtn, "设置三轴直线插补失败!");
        }

        public void StopCrd(short CardNo, bool now)
        {
            short rtn = 0;
            rtn += csLTDMC.LTDMC.dmc_stop_multicoor((ushort)CardNo, 0, 0);
        }
    }
}
